package com.oms.wtj;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;


public class EntityBeanTool extends ExecuteSql {

	public List<String> getTableList() {
		List<Map<String, Object>> list = super.queryForList("show full tables");
		List<String> result = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Map m = (Map) list.get(i);
			System.out.println();
			String tableName = (String) m.get("Tables_in_test");
			String tableType = (String) m.get("Table_type");
			if(tableType.equals("BASE TABLE")){
				result.add(tableName);
				System.out.println(tableName);
			}
		}
		
		return result;
	}

	public List<List<String>> getColumnList(String tableName) {
		List<Map<String, Object>> list = super.queryForList("show full fields from " + tableName);
		List<List<String>> result = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			List<String> row = new ArrayList();
			Map m = (Map) list.get(i);
			String field = (String) m.get("Field");
			String type = (String) m.get("Type");
			row.add(field);
			row.add(type);
			result.add(row);
		}
		return result;
	}
	
	public Map<String, List<String>> getColumnListForMap(String tableName) {
		List<Map<String, Object>> list = super.queryForList("show full fields from " + tableName);
		Map<String, List<String>> result = new HashMap();
		for (int i = 0; i < list.size(); i++) {
			List<String> row = new ArrayList();
			Map m = (Map) list.get(i);
			String field = (String) m.get("Field");
			String type = (String) m.get("Type");
			String comment = (String) m.get("Comment");
			row.add(type);
			row.add(comment);
			result.put(field.toLowerCase(), row);
		}
		return result;
	}	

	public void createEntity(){
		List tables = getTableList();
		for(int i=0; i<tables.size(); i++){
			String tableName = (String)tables.get(i);
			String[] ts = StringUtils.split(tableName, "_");
			String tableName2 = "";
			for(int j=0; j<ts.length; j++){
				tableName2 += ts[j].substring(0,1).toUpperCase()+ts[j].substring(1);
			}
			File file = new File("D:\\workspace\\OMS\\src\\com\\oms\\app\\user\\model\\"+tableName2+".java");
			
			StringBuffer content = new StringBuffer();
			content.append("package com.oms.app.user.model;\n\n");
			content.append("import java.util.*;\n\n");
			content.append("public class "+tableName2+"{\n\n");
			
			List columns = getColumnList(tableName);
			for(int j=0; j<columns.size(); j++){
				List column = (List)columns.get(j);
				String field = ((String)column.get(0)).toLowerCase();
				String type = (String)column.get(1);
				
				String[] cs = StringUtils.split(field, "_");
				String field2 = "";
				for(int k=0; k<cs.length; k++){
					if(k==0){
						field2 = cs[0];
					}else{
						field2 += cs[k].substring(0,1).toUpperCase()+cs[k].substring(1);
					}
				}
		
				if(type.toLowerCase().startsWith("int")){
					if(field.equalsIgnoreCase("del_flag")){
						content.append("	private Integer "+field2+" = new Integer(0);\n");
					}else{
						content.append("	private Integer "+field2+";\n");
					}
				}else if(type.toLowerCase().startsWith("bigint")){
					content.append("	private Long "+field2+";\n");
				}else if(type.toLowerCase().startsWith("varchar")){
					content.append("	private String "+field2+";\n");
				}else if(type.toLowerCase().startsWith("float")){
					content.append("	private Float "+field2+";\n");
				}else if(type.toLowerCase().startsWith("datetime") || type.toLowerCase().startsWith("date")){
					content.append("	private Date "+field2+";\n");
				}else if(type.toLowerCase().startsWith("double")){
					content.append("	private Double "+field2+";\n");
				}
			}
			
			content.append("\n");
			for(int j=0; j<columns.size(); j++){
				List column = (List)columns.get(j);
				String field = ((String)column.get(0)).toLowerCase();
				String type = (String)column.get(1);
				
				String[] cs = StringUtils.split(field, "_");
				String field2 = "";
				for(int k=0; k<cs.length; k++){
					field2 += cs[k].substring(0,1).toUpperCase()+cs[k].substring(1);
				}
		
				if(type.toLowerCase().startsWith("int")){
					content.append("	public Integer get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");	
					
					content.append("	public void set"+field2+"(Integer obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}else if(type.toLowerCase().startsWith("bigint")){
					content.append("	public Long get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");
					
					content.append("	public void set"+field2+"(Long obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}else if(type.toLowerCase().startsWith("varchar")){
					content.append("	public String get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");

					content.append("	public void set"+field2+"(String obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}else if(type.toLowerCase().startsWith("float")){
					content.append("	public Float get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");
					
					content.append("	public void set"+field2+"(Float obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}else if(type.toLowerCase().startsWith("datetime") || type.toLowerCase().startsWith("date")){
					content.append("	public Date get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");
					
					content.append("	public void set"+field2+"(Date obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}else if(type.toLowerCase().startsWith("double")){
					content.append("	public Double get"+field2+"(){\n");
					content.append("		return this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+";\n");
					content.append("	}\n");
					
					content.append("	public void set"+field2+"(Double obj){\n");
					content.append("		this."+field2.substring(0,1).toLowerCase()+field2.substring(1)+"=obj;\n");
					content.append("	}\n");
				}
			
			}
			content.append("}");
			try {
				FileUtils.writeStringToFile(file, content.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setTableBeanDictionary(){
		List tables = getTableList();
		File file = new File("D:\\workspace\\OMS\\src\\com\\oms\\app\\dictionary\\TableEntityBeanDictionary.java");
		StringBuffer content = new StringBuffer();
		content.append("package com.oms.app.dictionary;\n\n");
		content.append("import java.util.*;\n\n");
		content.append("public class TableEntityBeanDictionary{\n\n");
		
		for(int i=0; i<tables.size(); i++){
			String tableName = (String)tables.get(i);
			content.append("	public static final String "+tableName+" = \""+tableName+"\";\n");
		}
		
		content.append("	private static Map tables = new HashMap();\n\n");
		content.append("	private static Map columns;\n\n");
		
		content.append("	static{\n");		
		for(int i=0; i<tables.size(); i++){
			String tableName = (String)tables.get(i);
			String[] ts = StringUtils.split(tableName, "_");
			String tableName2 = "";
			for(int j=0; j<ts.length; j++){
				tableName2 += ts[j].substring(0,1).toUpperCase()+ts[j].substring(1);
			}
			content.append("		columns = new HashMap();\n");
			content.append("		tables.put(\""+tableName2+"Bean\", columns);\n");
			content.append("		columns.put(\"database_table_name\",\""+tableName+"\");\n");
			List columns = getColumnList(tableName);
			for(int j=0; j<columns.size(); j++){
				List column = (List)columns.get(j);
				String field = ((String)column.get(0)).toLowerCase();
				String[] cs = StringUtils.split(field, "_");
				String field2 = "";
				for(int k=0; k<cs.length; k++){
					if(k==0){
						field2 = cs[0];
					}else{
						field2 += cs[k].substring(0,1).toUpperCase()+cs[k].substring(1);
					}
				}
				content.append("		columns.put(\""+field2+"\", \""+field+"\");\n");
			}
			content.append("\n");
		}
		content.append("	}\n\n");
		
		content.append("	public static Map getTableFiledsByEntityName(String entityName){\n");
		content.append("		entityName = entityName.substring(entityName.lastIndexOf(\".\")+1);\n");
		content.append("		return (Map)tables.get(entityName);\n");
		content.append("	}\n\n");
		

		content.append("	public static Map getTableFiledsByBean(Object obj){\n");
		content.append("		String entityName = obj.getClass().toString();\n");
		content.append("		entityName = entityName.substring(entityName.lastIndexOf(\".\")+1);\n");		
		content.append("		return (Map)tables.get(entityName);\n");
		content.append("	}\n\n");
		
		content.append("	public static Map getBeanTableList(){\n");
		content.append("		Map result = new HashMap();\n");
		content.append("		Set set = tables.keySet();\n");
		content.append("		Iterator iterator  = set.iterator();\n");
		content.append("		while(iterator.hasNext()){\n");
		content.append("			Object key = iterator.next();\n");
		content.append("			Map columns = (Map)tables.get(key);\n");
		content.append("			Object tableName = columns.get(\"database_table_name\");\n");
		content.append("			result.put(key, tableName);\n");
		content.append("		}\n");
		content.append("		return result;\n");
		content.append("	}\n");
		content.append("}");
		try {
			FileUtils.writeStringToFile(file, content.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public void createMain(){
		createEntity();
		//setTableBeanDictionary();
	}
}
